package dk.jwood.android.graph;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import dk.jwood.android.graph.DataProvider.ValueDescriptor;
import dk.jwood.android.graph.DataProvider.ValueDescriptorImpl;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.util.Log;

public class DefaultDataProviderManager implements DataProviderManager {

	private static final String TAG = "DataProviderManager";

	private Map<String, DataProvider> dataProviders;
	
	private List<DataProviderManagerListener> listeners;
	
	private SharedPreferences sharedPrefs;
	
	
	public DefaultDataProviderManager(Context context) {
		
		dataProviders = new HashMap<String, DataProvider>();
		
		listeners = new LinkedList<DataProviderManagerListener>();
		
		
		sharedPrefs = context.getSharedPreferences("SATMON_SHARED_SETUP_DATA", Context.MODE_PRIVATE);
		
	}
	
	
	public List<DataProvider> getDataProviders() {
		return new ArrayList<DataProvider>(dataProviders.values());
	}
	
	public DataProvider getDataProvider(String id) {
		return dataProviders.get(id);
	}
	

	public void addDataProvider(DataProvider dp) {
		if(!dataProviders.containsKey(dp.getId())) {
			dataProviders.put(dp.getId(), dp);
			for(DataProviderManagerListener listener : listeners) {
				listener.dataProviderAdded(dp);
			}
		}
		
		
	}
	
	
	/* (non-Javadoc)
	 * @see dk.jwood.android.graph.DataProviderManager#addDataPoints(java.lang.String, long, float)
	 */
	@Override
	public void addDataPoints(String id, long timestamp, float ... values) {
		
		DataProvider dp = dataProviders.get(id);
		if(dp == null) {
			Log.d(TAG, "new provider id="+id+" creating new DataProvider...");
			dp = new DataProvider(id, values.length, this);
			loadPrefs(dp);
			addDataProvider(dp);
//			dataProviders.put(id, dp);
			Log.d(TAG, "created new DataProviderManager "+dp);
		}
		dp.addDataPoint(timestamp, values);
		
		
	}
	
	
	/* (non-Javadoc)
	 * @see dk.jwood.android.graph.DataProviderManager#addListener(dk.jwood.android.graph.DefaultDataProviderManager.DataProviderManagerListener)
	 */
	@Override
	public void addListener(DataProviderManagerListener listener) {
		if(!listeners.contains(listener)) {
			listeners.add(listener);
		}
	}
	
	/* (non-Javadoc)
	 * @see dk.jwood.android.graph.DataProviderManager#removeListener(dk.jwood.android.graph.DefaultDataProviderManager.DataProviderManagerListener)
	 */
	@Override
	public void removeListener(DataProviderManagerListener listener) {
		listeners.remove(listener);
	}
	
	/* (non-Javadoc)
	 * @see dk.jwood.android.graph.DataProviderManager#savePrefs(dk.jwood.android.graph.DataProvider)
	 */
	@Override
	public void savePrefs(DataProvider dataProvider) {
		
		synchronized (sharedPrefs) {
			Editor e = sharedPrefs.edit();
			
			e.putString(dataProvider.getId()+"-name", dataProvider.getName());
			
			for(ValueDescriptor vd : dataProvider.getValueDescriptors()) {
				e.putString(dataProvider.getId()+"-"+vd.getIndex()+"-label", vd.getLabel());
				e.putInt(dataProvider.getId()+"-"+vd.getIndex()+"-color", vd.getColor());
			}
			
			e.commit();
		}
		
		
		
	}
	
	/* (non-Javadoc)
	 * @see dk.jwood.android.graph.DataProviderManager#loadPrefs(dk.jwood.android.graph.DataProvider)
	 */
	@Override
	public void loadPrefs(DataProvider dataProvider) {
		
		synchronized (sharedPrefs) {
			
			String name = sharedPrefs.getString(createKey(dataProvider, "name"), dataProvider.getId());
			dataProvider.setName(name);
			
			
			for(ValueDescriptorImpl vd : dataProvider.getValueDescriptors()) {
//				e.putString(dataProvider.getId()+"-"+vd.getIndex()+"-label", vd.getLabel());
				String baseIndex = createKey(dataProvider, vd.getIndex()+"");
				
				String label = sharedPrefs.getString(baseIndex+"-label", baseIndex);
				if(label != null) vd.setLabel(label);
				
				int color = sharedPrefs.getInt(baseIndex+"-color", Color.GREEN);
				vd.setColor(color);
				
				
			}
			
			
		}
		
		
		
	}
	
	private final String createKey(DataProvider dp, String postfix) {
		return dp.getId()+"-"+postfix;
	}
	
	
}
